# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# MERIAM Project IHEID Team
#
# Kenya Paper
# Data Construction and Descriptives
# Module: Geocoding
#
# Author: Mirko Reul
#
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

######################## Geocoding ----
geocode_df <- function(datframe) {
  ### Recode ADM1 names
  datframe$adm1 <- dplyr::recode(datframe$adm1,
                           "Tana river" = "Tana River",
                           "West pokot" = "West Pokot",
                           "Tharaka Nithi" = "Tharaka-Nithi")
  
  ### Re-assign ADM1 names
  datframe <- datframe %>% 
    mutate(
      adm1 = replace(adm1, adm3 == "Kamarandi", "Embu"), # assigned Tharaka Nithi -> Embu based on GeoNames classification as location at https://www.geonames.org/11981656/kamarandi.html
      adm1 = replace(adm1, adm3 == "Nanighi", "Garissa"), # assigned Tana River -> Garissa based on GADM definition (see shapefile kenya_adm3)
      # adm1 = replace(adm1, adm3 == "Kapenguria", "West Pokot"), # assigned Baringo -> West Pokot (Nina geocoding)
      # adm1 = replace(adm1, adm3 == "Riwo", "Baringo") # assigned West Pokot -> Baringo (Nina geocoding)
    )
  
  ### Recode ADM3 names
  datframe$adm3 <- dplyr::recode(datframe$adm3,
                           "Komolion" = NA_character_,
                           "Ribko" = "Ribkwo",
                           "Orus" = NA_character_,
                           "Saimo Soi" = "Saimo/Soi",
                           "Akoret" = NA_character_,
                           "Kanyuambora" = NA_character_,
                           "Kamarandi" = NA_character_,
                           "Abakayle" = "Abakaile",
                           "Oldonyiro" = "Oldo/Nyiro",
                           "Charri" = "Chari",
                           "Bula Pesa" = "Bulla Pesa",
                           "Loodokilani" = "Iloodokilani",
                           "Ewuaso Kidong" = "Ewuaso Oonkidong'I",
                           "Mbirikani" = "Mbirikani/Eselenkei",
                           "Oldonyonyokie" = "Keekonyokie",
                           "KANYANGI" = "Kanyangi",
                           "Chengoni-Samburu" = "Chengoni/Samburu",
                           "Waa-Ng'ombeni" = "Waa",
                           "Kasemeni" = "Kinango", # triangulation via https://wikitionary254.com/kwale-county-sub-counties/, figure 2.1 https://www.researchgate.net/publication/328485238_Determinants_of_Women_Participation_in_Livelihood_Development_Activities_A_Case_of_Kinango_Sub-County_Kwale_County_Kenya/link/5c063165a6fdcc315f9b0f32/download
                           "Mukogodo East" = "Mugogodo East",
                           "Mukogodo West" = "Mugogodo West",
                           "Baharini" = "Bahari",
                           "NGUU/MASAMBA" = "Nguu/Masumba",
                           "KASIKEU" = "Kasikeu",
                           "MUKAA" = "Mukaa",
                           "Simbir Fatuma" = "Shimbir Fatuma",
                           "Banisa" = "Banissa",
                           "Warankara" = "Waranqara",
                           "Rhamu Dimtu" = "Rhamu-Dimtu",
                           "Sagante" = "Sagante/Jaldesa",
                           "Korr" = "Korr/Ngurunit",
                           "Heilu Manyatta" = "Heillu/Manyatta",
                           "Akithi" = "Akithii",
                           "Amwitha" = "Amwathi",
                           "Naroosura" = "Majimoto/Naroosura",
                           "Thegu river" = "Thegu River",
                           "Suguta Mar Mar" = "Suguta Marmar",
                           "Wusi" = "Wusi/Kishamba",
                           "Challa" = "Chala",
                           "Wumingu" = "Wumingu/Kishushe",
                           "Tunyai" = NA_character_,
                           "Kamarandi" = NA_character_,
                           "Kalemng'orok" = "Katilu",
                           "Lokori/Kachodin" = "Lokiriama/Lorengippi",
                           "Lorugum" = "Turkwel",
                           "Nachukui" = "Kalokol",
                           "Hadado" = "Hadado/Athibohol",
                           "Lagbogol" = "Lagboghol South",
                           "Barwaqo" = "Barwago",
                           "Dasheq" = "Tarbaj",
                           "Elnur" = "Elnur/Tula Tula",
                           "Riwo" = "Lokori/Kochodin",
                           "Masol" = "Masool")
  cat(paste0("Geocoding drops ", print(nrow(datframe[is.na(datframe$adm3),])), " observations from the analysis."))
  datframe <- subset(datframe, !is.na(datframe$adm3))
  return(datframe)
}
